import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import i18n from './i18n'
import './assets/fonts/iconfont.css'
import { Loading, Overlay, NavBar, Tabbar, TabbarItem, Tab, Tabs, Swipe, SwipeItem, Icon, Col, Row, Sidebar, SidebarItem, Toast } from 'vant'
import { getModule } from './utils'

// 获取应用实例
const app = createApp(App).use(store).use(router).use(i18n)

// 引用vant里面的组件
app.use(Loading).use(Overlay).use(NavBar).use(Tabbar).use(TabbarItem).use(Tab).use(Tabs).use(Swipe).use(SwipeItem).use(Icon).use(Col).use(Row).use(Sidebar).use(SidebarItem).use(Toast)

function plusReady() {
  // 获取app是否为沉浸式状态栏模式
  let isY = plus.navigator.isImmersedStatusbar();
  if (isY) {
    // 获取状态栏高度
    let height = plus.navigator.getStatusbarHeight();
    store.commit('setHeight', height)
  }
  // 隐藏滚动条
  plus.webview.currentWebview().setStyle({ scrollIndicator: 'none' });
}
if (window.plus) {
  plusReady();
} else {
  document.addEventListener('plusready', plusReady, false);
}

// 监听窗口的变化改变程序中默认字体大小
window.onresize = () => {
  let htmlArr = window.getComputedStyle(window.document.documentElement)
  let fontSize = htmlArr['font-size'].replace(/px/g, '')
  store.commit('setFontSize', fontSize)
}

// 获取directives文件夹下的所有的指令
const directives = getModule(require.context('./directive', true, /\.js$/))
// 循环导入app中
for (const key in directives) {
  app.directive(key, directives[key])
}

// 获取component文件夹下所有的组件
const components = getModule(require.context('./components', true, /\.vue$/))
// 循环导入app中
for (const key in components) {
  app.component(key, components[key])
}

// 将应用实例的根组件挂载在提供的 DOM 元素上
app.mount('#app')
